package com.cetccloud.ap.util.trace;

import com.alibaba.fastjson.JSON;
import com.cetccloud.ap.proxy.channel.cossDomain.model.CrossDomainModel;
import com.cetccloud.ap.proxy.channel.cossDomain.model.FileXmlModel;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName TraceLogger
 * @Description TODO
 * @Author superz
 * @Date 2021/4/25 9:12
 * @Version 1.0
 **/
@Component
public class TraceLogger {

    private static final Logger logger = LoggerFactory.getLogger("trace");

    public static void traceSend(Trace trace, CrossDomainModel crossDomainModel) {
        Map<String, Object> logMap = new LinkedHashMap<>();
        logMap.put("time", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss.SSS"));
        logMap.put("type", "send");
        logMap.put("trace", trace);
        logMap.put("model", crossDomainModel);
        logger.trace(JSON.toJSONString(logMap));
    }

    public static void traceResp(Trace trace, CrossDomainModel crossDomainModel) {
        Map<String, Object> logMap = new LinkedHashMap<>();
        logMap.put("time", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss.SSS"));
        logMap.put("type", "resp");
        logMap.put("trace", trace);
        logMap.put("model", crossDomainModel);
        logger.trace(JSON.toJSONString(logMap));
    }

    public static void traceRecv(Trace trace, CrossDomainModel crossDomainModel) {
        Map<String, Object> logMap = new LinkedHashMap<>();
        logMap.put("time", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss.SSS"));
        logMap.put("type", "recv");
        logMap.put("trace", trace);
        logMap.put("model", crossDomainModel);
        logger.trace(JSON.toJSONString(logMap));
    }

    public static void traceFileXml(Trace trace, FileXmlModel fileXmlModel) {
        Map<String, Object> logMap = new LinkedHashMap<>();
        logMap.put("time", DateFormatUtils.format(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss.SSS"));
        logMap.put("type", "fileXml");
        logMap.put("trace", trace);
        logMap.put("model", fileXmlModel);
        logger.trace(JSON.toJSONString(logMap));
    }
}
